#!/usr/bin/env python
#coding:utf-8
#
# Copyright 2010 Close To U.
#
# Author:Darcy<http://closetou.com>
#

class Pagination(object):
  def __init__(self,record_count,current = 1,page_size = 10):    
    self.pagination = {}
    self.pagination['record_count'] = record_count
    self.pagination['current'] = current
    self.pagination['page_size'] = page_size    
    self.pagination['num_pages'] = int((self.pagination['record_count']+self.pagination['page_size']-1) / self.pagination['page_size'])
    self.pagination['page_range'] = []
    self.pagination['has_previous'] = None
    self.pagination['has_next'] = None    
    self.pagination['start_page'] = None
    self.pagination['end_page'] = None
    self.pagination['previous_ellipse'] = False
    self.pagination['next_ellipse'] = False

    if self.pagination['current'] < 1:
      self.pagination['current'] = 1
      
    if self.pagination['current'] > self.pagination['num_pages']:
      self.pagination['current'] = self.pagination['num_pages']

    self.pagination['start_index'] = self.pagination['page_size']*(self.pagination['current']-1) +1
    self.pagination['end_index'] = self.pagination['start_index'] + self.pagination['page_size'] -1
      
    if self.pagination['current'] >1:
      self.pagination['has_previous'] = self.pagination['current'] - 1

    if self.pagination['current'] < self.pagination['num_pages']:
      self.pagination['has_next'] = self.pagination['current'] + 1

    if self.pagination['num_pages'] <= 10:
      for i in range(1,self.pagination['num_pages']+1):
        self.pagination['page_range'].append(i)
    else:
        self.pagination['page_range'].append(1)
        if (self.pagination['current'] - 4) >2 and (self.pagination['current'] + 4) <self.pagination['num_pages']:
          start_page = self.pagination['current'] - 4
        elif (self.pagination['current'] + 4) >= self.pagination['num_pages']:
          start_page = self.pagination['num_pages']- 8
        else:
          start_page = 2
        end_page = start_page + 7
        for i in range(0,8):
          self.pagination['page_range'].append(start_page + i)
        
        self.pagination['page_range'].append(self.pagination['num_pages'])
        self.pagination['start_page'] = start_page
        self.pagination['end_page'] = end_page
        if start_page > 2:
          self.pagination['previous_ellipse'] = True
        if end_page < self.pagination['num_pages'] -1:
          self.pagination['next_ellipse'] = True
  def pagination(self):
    return self.pagination
  
  def Test(self):
    print 'Pagination Test'
    print self.pagination
    
if '__main__' == __name__:
  test = Pagination(100,5)
  test.Test()
